<template>
  <div style="width: 100%; height: 100%;">
    <el-container>
<!--      <el-header style="padding: 0 10%; box-shadow: none;">-->

<!--      </el-header>-->

      <el-main style="display: flex;justify-content: center; padding-left: 0; padding-right: 0;">
        <!--题目-->
        <el-card style="width: 75%; text-align: left">
          <div slot="header" style="text-align: left;">
            <strong>{{ currentPage }}</strong>
            {{ mistake[currentPage - 1]?.questionTitle || '暂无数据' }}
          </div>
          <div>{{ mistake[currentPage - 1]?.questionContent }}</div>
          <el-image v-if="mistake[currentPage-1]?.questionImage"
                    :src="mistake[currentPage-1]?.questionImage"
                    fit="contain" style="width: 100%; height: 80vh;"
                    :preview-src-list="[mistake[currentPage-1]?.questionImage]"></el-image>
        </el-card>

        <!--功能栏-->
        <el-card style="width: 18%; padding: 0;">

          <el-radio-group v-model="mode" @input="getMistake" style="margin-bottom: 30px">
            <el-radio-button label="random">随机练题</el-radio-button>
            <el-radio-button label="ordered">顺序练题</el-radio-button>
          </el-radio-group>

          <div style="display: flex;margin-bottom: 10px; align-items: center;">
            <el-select @change="getMistake" v-model="count" placeholder="请选择训练题目数量" filterable
                       style="flex: 2;">
              <el-option
                  v-for="item in [5,10,20,50]"
                  :key="item"
                  :label="'练题数量：'+item"
                  :value="item">
              </el-option>
            </el-select>

            <div style="flex: 1;">共 {{ total }} 题</div>
          </div>

          <el-pagination
              :current-page="currentPage"
              @current-change="handleCurrentChange"
              :page-size="1"
              layout="pager"
              :total="total"
              :pager-count="50"
              background
              style="width: 100%">
            <!--          <div slot="prev">ddd</div>-->
          </el-pagination>
          <el-pagination
              :current-page="currentPage"
              @current-change="handleCurrentChange"
              :page-size="1"
              layout="prev,next"
              :total="total"
              :pager-count="50"
              background
              style="width: 100%; margin-top: 10px">
            <!--          <div slot="prev">ddd</div>-->
          </el-pagination>
          <el-button round @click="explain=true" type="primary" style="margin-top: 20px">解析</el-button>
        </el-card>
      </el-main>

    </el-container>

    <!--   答案解析抽屉   -->
    <el-drawer
        :with-header="false"
        :visible.sync="explain"
        direction="rtl">
      <el-card class="explain">
        <div slot="header">答案</div>
        <div>
          {{ mistake[currentPage - 1]?.questionAnswer || '暂无数据' }}
        </div>
      </el-card>

      <el-card class="explain">
        <div slot="header">解析</div>
        <div>
          {{ mistake[currentPage - 1]?.questionAnalysis || '暂无数据' }}
        </div>
      </el-card>

      <el-card class="explain">
        <div slot="header">总结</div>
        <div>{{ mistake[currentPage - 1]?.questionSummary || '暂无数据' }}</div>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import service from "@/utils/httpRequest";

export default {
  name: 'PracticePage',
  data() {
    return {
      mode: 'random',
      count: 5,
      randomSend: {
        count: 5//默认一次随机练题需要五个随机题目
      },
      orderedSend: {
        current: 1,
        pageSize: 5//默认一次顺序练题需要五个题目
      },
      mistake: [],
      total: 0,
      explain: false,
      currentPage: 1,
    }
  },
  mounted() {
    this.getRandom()
  },
  methods: {
    //请求随机题目
    getRandom() {
      service.get('/question/random', {params: this.randomSend}).then(res => {
        if (res.code === 1) {
          this.currentPage = 1
          this.mistake = res.data.records
          this.total = res.data.total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    //请求顺序题目
    getOrdered() {
      service.get('/question/list', {params: this.orderedSend}).then(res => {
        if (res.code === 1) {
          this.currentPage = 1
          this.mistake = res.data.records
          this.total = Math.min(res.data.total, this.orderedSend.pageSize)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    //获取题目，切换练题模式和改变题目训练数量都会触发
    getMistake() {
      this.randomSend.count = this.count
      this.orderedSend.pageSize = this.count
      if (this.mode === 'random') {
        this.getRandom()
      } else if (this.mode === 'ordered') {
        this.getOrdered()
      } else {
        //未定义情况
        this.$message.error('未知错误')
      }
    },
    //跳转至某一题
    handleCurrentChange(val) {
      this.currentPage = val
    }
  }
}
</script>

<style scoped>
.explain {
  width: 90%;
  text-align: left;
  margin: 20px 3%;
}

/*分页组件的样式*/
/*/deep/.el-pagination span:not([class*=suffix]){
    font-size: 50px!important;
}*/

/deep/ .el-pager {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}

</style>
